package com.food.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.food.user.entity.StoreInfo;
import com.food.user.form.EsForm;
import com.food.user.mapper.StoreInfoMapper;
import com.food.user.model.StoreInfoModel;
import com.food.user.service.StoreInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商家信息 服务实现类
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StoreInfoService {
    @Autowired
    private ElasticsearchRestTemplate ert;

    @Override
    public  List<StoreInfoModel> esList() {
        NativeSearchQueryBuilder nsqb=new NativeSearchQueryBuilder();
        NativeSearchQuery nsq=nsqb.build();

        SearchHits<StoreInfoModel> search = ert.search(nsq, StoreInfoModel.class);
        List<SearchHit<StoreInfoModel>> searchHits = search.getSearchHits();
        List<StoreInfoModel> storeInfoModels = new ArrayList<>();
        for (SearchHit<StoreInfoModel> searchHit : searchHits) {
            StoreInfoModel content = searchHit.getContent();
            storeInfoModels.add(content);
        }
        return storeInfoModels;
    }

    @Override
    public StoreInfoModel detail(Long storeInfoId) {

        Criteria c = new Criteria("storeInfoId").is(storeInfoId);
        CriteriaQuery cq = new CriteriaQuery(c);
        SearchHits<StoreInfoModel> search = ert.search(cq, StoreInfoModel.class);
        List<SearchHit<StoreInfoModel>> searchHits = search.getSearchHits();
        StoreInfoModel content=null;
        for (SearchHit<StoreInfoModel> searchHit : searchHits) {
           content = searchHit.getContent();
        }
        return content;
    }

    @Override
    public List<StoreInfoModel> condition(EsForm form) {
        NativeSearchQueryBuilder nsqb=new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        List<QueryBuilder> should = boolQueryBuilder.should();
        should.add(new MatchQueryBuilder("storeInfoName",form.getKeyword()));
        should.add(new MatchQueryBuilder("storeInfoAddress",form.getKeyword()));
        should.add(new MatchQueryBuilder("storeInfoStoreIntro",form.getKeyword()));
        nsqb.withQuery(boolQueryBuilder);

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("storeInfoStoreIntro");
        highlightBuilder.preTags("<span style='color:red'>");
        highlightBuilder.postTags("</span>");
        highlightBuilder.fragmentSize(30);
        highlightBuilder.numOfFragments(1);
        nsqb.withHighlightBuilder(highlightBuilder);

        NativeSearchQuery nsq=nsqb.build();
        SearchHits<StoreInfoModel> search = ert.search(nsq, StoreInfoModel.class);
        List<StoreInfoModel> storeInfoModels=new ArrayList<>();
        for (SearchHit<StoreInfoModel> searchHit : search.getSearchHits()) {
            StoreInfoModel content = searchHit.getContent();
            if (searchHit.getHighlightFields()!=null&&searchHit.getHighlightFields().size()>0){
                if (searchHit.getHighlightFields().get("storeInfoStoreIntro")!=null){
                    content.setStoreInfoStoreIntro(searchHit.getHighlightFields().get("storeInfoStoreIntro").get(0));
                }
            }else {
                if (content.getStoreInfoStoreIntro().length()>=30){
                    content.setStoreInfoStoreIntro(content.getStoreInfoStoreIntro().substring(0,29));
                }
            }
            storeInfoModels.add(content);
        }
        return storeInfoModels;
    }

    @Override
    public List<StoreInfoModel> type(Long typeId) {

        NativeSearchQueryBuilder nsqb=new NativeSearchQueryBuilder();
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        List<QueryBuilder> should = boolQueryBuilder.should();
        should.add(new MatchQueryBuilder("foodType.typeId",typeId));
        nsqb.withQuery(boolQueryBuilder);
        NativeSearchQuery nsq=nsqb.build();
        SearchHits<StoreInfoModel> search = ert.search(nsq, StoreInfoModel.class);
        List<SearchHit<StoreInfoModel>> searchHits = search.getSearchHits();
        List<StoreInfoModel> storeInfoModels = new ArrayList<>();
        for (SearchHit<StoreInfoModel> searchHit : searchHits) {
            StoreInfoModel content = searchHit.getContent();
            storeInfoModels.add(content);
        }
        return storeInfoModels;
    }
}
